﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.ComponentModel;
using System.IO.Ports;
using VIA.Integration;
using VIA.Integration.Domain;
using VIA.Integration.LeadShine;
using VIA.Integration.Modbus;
using VIA.Presentation;
using VIA.Project.Domain.Services;
using VIA.SiMoJi.Domain.Electronics;

namespace VIA.SiMoJi.Domain.Inspection
{

    public partial class Inspector
    {
        #region BOM

        private Electronic Electronic => Electronic.Default;
        public BuJinPiDai BuJinPiDai => GongLiaoStation.PiDai;
        public GongLiaoStation GongLiaoStation { get; }
        public VisionStation VisionStation { get; }
        public BanYunStation BanYunStation { get; }
        public SiMoStation SiMoStation { get; }
        public NGXiaLiao NGXiaLiao { get; }
        public Feeder Feeder1 { get; }
        public Feeder Feeder2 { get; }
        public LightButton StartButton { get; }
        public LightButton StopButton { get; }
        public LightButton ResetButton { get; }
        public Panel Panel { get; }
        public EventButton EMGButton { get; }
        public EventButton FeederButton1 { get; }
        public EventButton FeederButton2 { get; }
        public EventButton ManualModeButton { get; }
        public LightButton FeederCloseSensor1 { get; }
        public LightButton FeederCloseSensor2 { get; }
        public EventButton NGOpenButton { get; }
        public EventButton NGCloseButton { get; }
        public SafetyDoors SafetyDoors { get; }
        public Door Door1 { get; }
        public Door Door2 { get; }

        public ModbusRtuPLC Modbus = new ModbusRtuPLC() { Option = new ModbusRtuOption() { BaudRate = 115200, DataBits = 8, PortName = "COM1", Station = 0 } };

        public SerialPort SerialPort = new SerialPort() { BaudRate = 115200, DataBits = 8, StopBits = StopBits.One, PortName = "COM1", Parity = Parity.None };

        public AirSource AirSource { get; }

        #endregion

        #region Axies

        [MotionPath("运行高速")]
        [MotionPath("运行低速")]
        public DmcAxis FeedAxis1 { get; }
        [MotionPath("运行高速")]
        [MotionPath("运行低速")]
        public DmcAxis FeedAxis2 { get; }

        #endregion

        #region Events
        public event EventHandler<ManualAutoStateChangedEventArgs> ManualAutoStateChanged;
        #endregion

        public Inspector(IServiceProvider service, ILoggerFactory loggerFactory, ICurrentRecipe recipe)
        {
            FoxconnMES = new FoxconnMES() { Name = "MES客户端", Id = nameof(FoxconnMES) };
            Add(FoxconnMES);
            AirSource = new AirSource(Electronic.DI08_总气源气压表) { Name = "气压总表", Id = nameof(AirSource) };
            StartButton = new LightButton(Electronic.DI01_面板启动按钮, Electronic.DO01_面板启动按钮灯) { Name = "面板启动按钮", Id = nameof(StartButton) };
            StopButton = new LightButton(Electronic.DI02_面板停止按钮, Electronic.DO02_面板停止按钮灯) { Name = "面板停止按钮", Id = nameof(StopButton) };
            ResetButton = new LightButton(Electronic.DI03_面板复位按钮, Electronic.DO03_面板复位按钮灯) { Name = "面板复位按钮", Id = nameof(ResetButton) };
            EMGButton = new EventButton(Electronic.DI06_面板急停按钮) { Name = "面板急停按钮", Id = nameof(EMGButton) };
            ManualModeButton = new EventButton(Electronic.DI04_手动自动旋钮手动) { Name = "手动自动旋钮手动", Id = nameof(ManualModeButton) };
            ManualModeButton.Pressed += (s, e) => { Stop(); OnManualAutoStateChanged(EquipmentManualAutoState.Manual); };
            ManualModeButton.Released += (s, e) => OnManualAutoStateChanged(EquipmentManualAutoState.Auto);
            FeederButton1 = new EventButton(Electronic.DI52_飞达1抽屉按钮) { Name = "飞达1抽屉按钮", Id = nameof(FeederButton1) };
            FeederButton2 = new EventButton(Electronic.DI56_飞达2抽屉按钮) { Name = "飞达2抽屉按钮", Id = nameof(FeederButton2) };
            FeederCloseSensor1 = new LightButton(Electronic.DI83_飞达1抽屉闭合光电, Electronic.DO52_飞达1抽屉按钮灯);
            FeederCloseSensor2 = new LightButton(Electronic.DI84_飞达2抽屉闭合光电, Electronic.DO56_飞达2抽屉按钮灯);
            NGOpenButton = new EventButton(Electronic.DI61_NG抽屉按钮);
            NGCloseButton = new EventButton(Electronic.DI62_NG抽屉闭合光电);
            Panel = new Panel(StartButton, StopButton, ResetButton, EMGButton) { Name = "信号" };
            Door1 = new Door(Electronic.DI09_门禁报警信号1) { Name = "门禁报警信号1", Id = nameof(Door1) };
            Door2 = new Door(Electronic.DI10_门禁报警信号2) { Name = "门禁报警信号2", Id = nameof(Door2) };
            SafetyDoors = new SafetyDoors(Door1, Door2, Electronic.DO09_门禁锁电源1, Electronic.DO10_门禁锁电源2) { Name = "安全门" };
            NGXiaLiao = new(NGOpenButton, NGCloseButton) { Id = nameof(NGXiaLiao), Name = "NG下料" };
            VisionStation = new(service) { Id = nameof(VisionStation), Name = "视觉工位" };
            GongLiaoStation = new() { Id = nameof(GongLiaoStation), Name = "供料模组", LogGroup = LoggerCategory.GongLiao };
            BanYunStation = new(NGXiaLiao, FoxconnMES) { Id = nameof(BanYunStation), Name = "搬运模组", LogGroup = LoggerCategory.BanYun };
            FeedAxis1 = new DmcAxis(Electronic.Controller) { Id = nameof(FeedAxis1), NoId = 29, Name = "飞达1步进" };
            FeedAxis2 = new DmcAxis(Electronic.Controller) { Id = nameof(FeedAxis2), NoId = 30, Name = "飞达2步进" };

            this.Add(FeedAxis1, FeedAxis2);
            Feeder1 = new(FeedAxis1, Electronic.Cylinder_飞达1压膜气缸, Electronic.Cylinder_飞达1定位气缸, Electronic.DO52_飞达1抽屉按钮灯, FeederButton1, FeederCloseSensor1, Electronic.DI85_飞达1料带有无检测, Electronic.DI86_飞达1放置到位, Electronic.DI87_飞达1收料触发, Electronic.DI88_飞达1满料检测, true) { Id = nameof(Feeder1), Name = "飞达1组件" };
            Feeder2 = new(FeedAxis2, Electronic.Cylinder_飞达2压膜气缸, Electronic.Cylinder_飞达2定位气缸, Electronic.DO56_飞达2抽屉按钮灯, FeederButton2, FeederCloseSensor2, Electronic.DI89_飞达2料带有无检测, Electronic.DI90_飞达2放置到位, Electronic.DI91_飞达2收料触发, Electronic.DI92_飞达2满料检测, false) { Id = nameof(Feeder2), Name = "飞达2组件" };
            SiMoStation = new(Feeder1, Feeder2) { Id = nameof(SiMoStation), Name = "撕膜工位", LogGroup = LoggerCategory.SiMo };
            BanYunStation.Feeder1 = Feeder1;
            BanYunStation.Feeder2 = Feeder2;
            Add(AirSource);
            Add(GongLiaoStation);
            Add(BanYunStation, SiMoStation, VisionStation);
            Add(Feeder1, Feeder2);
            Add(NGXiaLiao);
            Add(Electronic.Controller);
            Add(Panel);
            //Add(Door1, Door2);
            Add(StartButton, StopButton, ResetButton, EMGButton);
            //Add(Electronic.AlarmLamp);
            Add(SafetyDoors);


            GongLiaoStation.Vision = VisionStation;
            BanYunStation.Vision = VisionStation;
            VisionStation.BanYunStation = BanYunStation;
            BanYunStation.GongLiaoStation = GongLiaoStation;
            BanYunStation.SiMoStation = SiMoStation;
            SiMoStation.BanYunStation = BanYunStation;
            SiMoStation.FeiDaStation1 = Feeder1;
            SiMoStation.FeiDaStation2 = Feeder2;
            Feeder1.SiMoStation = SiMoStation;
            Feeder2.SiMoStation = SiMoStation;
            NGXiaLiao.BanYunStation = BanYunStation;


            this.RefreshMode = RefreshMode.WhenInitialize;
            _applicationLog = service.GetRequiredService<IApplicationLog>();
            _messageService = service.GetRequiredService<IMessageService>();
            Logger = loggerFactory.CreateLogger("Inspector");
            Service = service;
            Name = "自动撕膜设备";
            Interval = 200;
            Version = GetVersion();
            InitMotionPaths(this, recipe);
            AlarmStatusChanged += Inspector_AlarmStatusChanged;

            //Refresh();
        }
        public FoxconnMES FoxconnMES { get; set; }
    }

    public enum EquipmentManualAutoState
    {
        [Description("手动中")]
        Manual,
        [Description("自动中")]
        Auto
    }
    public class ManualAutoStateChangedEventArgs : EventArgs
    {
        public EquipmentManualAutoState State { get; }

        public ManualAutoStateChangedEventArgs(EquipmentManualAutoState state)
        {
            State = state;
        }
    }

}
